package com.tony.jdbc.dynamic.service.impl;

import com.tony.jdbc.dynamic.annotation.DynamicDatasource;
import com.tony.jdbc.dynamic.datasource.DynamicDataSourceKeyHolder;
import com.tony.jdbc.dynamic.service.IDynamicAnnotationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tony
 * @desc 多数据源注解切换接口实现类
 * @date 2025/11/18
 */
@Service
public class DynamicAnnotationServiceImpl implements IDynamicAnnotationService {
    @Value("${spring.jta.enabled:false}")
    private boolean jta;
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    /**
     * 获取当前数据源的JdbcTemplate对象
     *
     * @return JdbcTemplate
     */
    private JdbcTemplate getCurrentJdbcTemplate() {
        JdbcTemplate currentJdbc;
        if (jta) {
            currentJdbc = applicationContext.getBean(DynamicDataSourceKeyHolder.getKey() + DynamicDataSourceKeyHolder.JDBC_TEMPLATE, JdbcTemplate.class);
        } else {
            currentJdbc = this.jdbcTemplate;
        }
        if (currentJdbc == null) {
            throw new RuntimeException("JdbcTemplate is null");
        }
        return currentJdbc;
    }

    /**
     * 获取当前数据源的NamedParameterJdbcTemplate对象
     *
     * @return JdbcTemplate
     */
    private NamedParameterJdbcTemplate getCurrentNamedParameterJdbcTemplate() {
        NamedParameterJdbcTemplate currentJdbc;
        if (jta) {
            currentJdbc = applicationContext.getBean(DynamicDataSourceKeyHolder.getKey() + DynamicDataSourceKeyHolder.NAMED_JDBC_TEMPLATE, NamedParameterJdbcTemplate.class);
        } else {
            currentJdbc = this.namedParameterJdbcTemplate;
        }
        if (currentJdbc == null) {
            throw new RuntimeException("NamedParameterJdbcTemplate is null");
        }
        return currentJdbc;
    }

    /**
     * 注解方式切换数据源，查询h2数据源下的用户表数据
     *
     * @return h2数据源下的用户表数据
     */
    @DynamicDatasource("h2-1")
    @Transactional(rollbackFor = Exception.class)
    public List<Map<String, Object>> getDataSourceH2() throws SQLException {
        return this.getCurrentJdbcTemplate().queryForList("select id,user_name,pwd,age from t_user");
    }

    /**
     * 注解方式切换数据源，查询derby数据源下的用户表数据
     *
     * @return derby数据源下的用户表数据
     */
    @DynamicDatasource("derby-1")
    @Transactional(rollbackFor = Exception.class)
    public List<Map<String, Object>> getDataSourceDerby() throws SQLException {
        return this.getCurrentNamedParameterJdbcTemplate().queryForList("select id,user_name,pwd,age from T_USER_DERBY", new HashMap<>());
    }

}
